<%@page import="com.wh.minihelp.user.domain.ChildInfo"%>
<%@page import="com.wh.minihelp.user.util.UserUtil"%>
<%@page import="com.wh.minihelp.user.domain.FamilyAccount"%>
<%@page import="com.wh.minihelp.flower.domain.FamilyFlower"%>
<%@page import="java.util.List"%>
<%@ page language="java" pageEncoding="UTF-8"%>

<div class="column_title">
	<span >红花类型列表</span> 
	<div class="column_title_oper"><a href="#" onclick="expand()">收起/展开</a></div>
</div>
<div id="div_flower" class="weui-cells weui-cells-ex">
<%
List<FamilyFlower> flowers = (List<FamilyFlower>)request.getAttribute("flowers");
size=flowers.size();
for(int i=0;i<size;i++){
	FamilyFlower ff=flowers.get(i);
%>
		<cc data="['<%=ff.getId()%>',<%=ff.getOrder()%>]" dt='<%=ff.getType()%>'>
            <div class="weui-cell weui-cell-ex">
                <c><%=(i+1)%></c><d>.[<%=(ff.getTypeString())%>]<%=ff.getName()%></d>
           	</div>
            <div class="weui-cell-oper">
            	<a href="#" onclick="delFlower()">删除</a> &nbsp;
            	<a href="#" onclick="changeOrder(-1)">上移</a> &nbsp;
            	<a href="#" onclick="changeOrder(1)">下移</a>
            </div>
        </cc>    
<%}%>
	<div class="weui-cells-oper"><a id="btnAddFlower" href="#" class="weui-btn weui-btn_mini weui-btn_primary">添加新红花类别</a></div>
</div>
<div style='display:none'>
	<div style='display:none' id="div_addFlower_none">
	  <div id='div_addFlower'>
			<div class="weui-tab">
				<div class="weui-navbar">
					<div id='0' class="weui-navbar__item weui-bar__item_on" style='padding:0px;line-height:36px'>
						<img src="<%=path%>/res/img/flower/add.png" style='vertical-align: middle;padding:0px 0px'></img>
						<span style='vertical-align: middle;'>加</span>
					</div>
					<div id='1' class="weui-navbar__item" style='padding:0px;line-height:36px'>
						<img src="<%=path%>/res/img/flower/sub.png" style='vertical-align: middle;padding:0px 0px'></img>
						<span style='vertical-align: middle;'>减</span>                   
					</div>
				</div>
				<div class="weui-tab__panel">
					<label class="weui-label"><B>类别名称:</B></label>
					<input class="weui-input" type="text" id="addFlowerName" placeholder="请输入红花类别名称">
				</div>
			</div>
	   </div>
	</div>
	<cc data="" dt="0" id="cc_flower">
		<div class="weui-cell weui-cell-ex"><c></c><d></d></div>
		<div class="weui-cell-oper">
			<a href="#" onclick="delFlower()" class="ui-link">删除</a> &nbsp;
			<a href="#" onclick="changeOrder(-1)" class="ui-link">上移</a> &nbsp;
			<a href="#" onclick="changeOrder(1)" class="ui-link">下移</a>
		</div>
	</cc>
</div>
<div style='padding:5px;'></div>
	
<script type="text/javascript">
$(function(){
	$('#btnAddFlower').on('click', function(){
		$("#js_dialog_title").text("添加新的红花类别"); 
		$("#js_dialog_content").append($("#div_addFlower"));
		okFunction=addFlower;
		cancelFunction=cancelFlower;
		$androidDialog1.css('display','')
	});
    $('#div_addFlower .weui-navbar__item').on('click', function () {
        $(this).addClass('weui-bar__item_on').siblings('.weui-bar__item_on').removeClass('weui-bar__item_on');
    });
});
	function cancelFlower(){
		$("#div_addFlower_none").append($("#div_addFlower"));
		$androidDialog1.css('display','none');
	}
	function addFlower(){		
		if ($.trim($("#addFlowerName").val()).length == 0) {
			error("#addFlowerName","请输入类别名称！")
			return
		}
		if(!checkInput($("#addFlowerName").val())){
			error("#addFlowerName","类别名称中有特殊字符，请重新输入！")
			return 
		}
		var type=$('#div_addFlower .weui-bar__item_on').attr('id');
		$.post("<%=path%>/f?m=add"
		  , { name: $("#addFlowerName").val(),type:type}	   
		  ,function(data){
				if(data.success){
					cancelFlower();	
					var last=$("#div_flower cc[dt="+type+"] :last");
					var newLi=null;
					if(last.length==0){
						newLi=$("#cc_flower").clone();
					}else{
						newLi=last.clone();
					}
					newLi.attr('data',"['"+data.value.id+"',"+data.value.order+"]");
					newLi.find("d").text(".["+data.value.typeString+"]"+data.value.name);
					if(last.length==0){
						if(data.value.type==0){// 如果是加操作
							newLi.insertBefore($("#div_flower :first"));
						}else{
							newLi.insertBefore($("#div_flower .weui-cells-oper"));
						}
					}else{
						newLi.insertAfter(last);
					}
					resortCC("div_flower");
				}else{
					alert(data.msg)
				}
		   }, "json");		
	}
	function delFlower(){
		var cur=$(event.srcElement).parents("cc");
		var data=eval(cur.attr("data"));
		$.post("<%=path%>/f?m=del", { id: data[0]}	   
		  ,function(data){
				if(data.success){
					cur.remove();
					resortCC("div_flower");
				}else{
					alert(data.msg)
				}
		   }, "json");		
	}
	function changeOrder(direction){
		var cur=$(event.srcElement).parents("cc");
		var data=eval(cur.attr("data"));
		if(direction==-1){
			var pre=cur.prev();
			if(pre.length==0){
				alert("已经是第一个了~");
				return;
			}
			var d2=eval(pre.attr("data"));
			if(cur.attr('dt')!=pre.attr('dt')){
				alert("加减的类型不同，就别移了~");
				return;				
			}
			data[1]=data[1]+direction;
			d2[1]=d2[1]-direction;
			$.post("<%=path%>/f?m=order", { flower1:data[0],order1:data[1],flower2:d2[0],order2:d2[1]}	   
			  ,function(data){
					if(data.success){
						cur.insertBefore(pre);
						resortCC("div_flower");
					}else{
						alert(data.msg)
					}
			   }, "json");		
		}else{
			var pre=cur.next();
			if(pre.length==0){
				alert("已经是最后第一个了~");
				return;
			}
			var d2=eval(pre.attr("data"));
			if(cur.attr('dt')!=pre.attr('dt')){
				alert("加减的类型不同，就别移了~");
				return;				
			}
			data[1]=data[1]+direction;
			d2[1]=d2[1]-direction;
			$.post("<%=path%>/f?m=order", { flower1:data[0],order1:data[1],flower2:d2[0],order2:d2[1]}	   
			  ,function(data){
					if(data.success){
						cur.insertAfter(pre);
						resortCC("div_flower");
					}else{
						alert(data.msg)
					}
			   }, "json");		
		}
	}
</script>